package org.example;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class FilesTest {
    public static void main(String[] args) throws IOException {
        // 这里“walk”的意思是查找每个子目录和文件，也就是遍历。
        // Files.walkFileTree使用了访问者（Visitor）模式
        Files.walkFileTree(Path.of("/home/one/Downloads/Share"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("即将进入目录[" + dir + "]");
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("访问文件[" + file + "]");
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                System.out.println("访问文件[" + file + "]失败");
                return FileVisitResult.CONTINUE;

            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("已经访问目录[" + dir + "]");
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
